﻿using AntdUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PosClient.Helper
{
    public class KeepFocusHelper
    {
        //Delegates for safe multi-threading.
        delegate void DelegateGetFocus();
        private DelegateGetFocus m_getFocus;
        static Thread newThread;
        static bool State = false;

        Control control;
        Control focusControl;
        string focusControlName;

        public KeepFocusHelper(Control control, Control focusControl)
        {
            this.control = control;
            this.focusControl = focusControl;
            this.focusControlName = focusControl.Name;
            m_getFocus = new DelegateGetFocus(this.getFocus);
        }

        public void SetFocusControl(Control control)
        {
            this.focusControl = control;
            this.focusControlName = control.Name;
        }

        public void SetParentControl(Control control)
        {
            this.control = control;
        }

        public void Start()
        {
            State = true;
            SpawnThread(keepFocus);
        }

        public void Stop()
        {
            State = false;
            newThread.Suspend();
        }

        public void Resume()
        {
            State = true;
            newThread.Resume();
        }


        //test focus stuff
        //Spawns a new Thread.
        private void SpawnThread(ThreadStart ts)
        {
            try
            {
                if (newThread == null)
                {
                    newThread = new Thread(ts);
                    newThread.Start();
                }
                if (newThread.ThreadState == ThreadState.Stopped)
                {
                    newThread.Start();
                }
            }
            catch (Exception e)
            {
            }
        }

        //Continuously call getFocus.
        private void keepFocus()
        {
            while (true)
            {
                if (State)
                {
                    Thread.Sleep(1000);
                    getFocus();
                }
            }
        }


        //Keeps Form on top and gives focus.
        private void getFocus()
        {
            //If we need to invoke this call from another thread.
            if (control.InvokeRequired)
            {
                try
                {
                    control.Invoke(m_getFocus, new object[] { });
                }
                catch (Exception)
                {
                }
            }
            //Otherwise, we're safe.
            else
            {
                if (this.focusControl.Name == focusControlName)
                {
                    if (this.focusControl is Input)
                    {
                        Input tb = (Input)this.focusControl;
                        if (!tb.Focused && !(tb.Text.Length > 0))
                        {
                            this.focusControl.Select();
                            this.focusControl.Focus();
                        }
                    }
                }
            }
        }
    }
}
